<?php
function html_log($msg, $stop, $start){
?>
  <!-- <?= $msg ?> :: <?= $stop - $start ?> seconds elapsed -->
<?php
}

$global_start = microtime(true);

$fullname = $_SESSION['first'].' '.$_SESSION['last'];
$imei = $_SESSION['imei'];

$tmp_start = microtime(true);
require_once $INCLUDE_DIR.'cametrics.helper.php';
//html_log('require helper', microtime(true), $tmp_start);


$url = Cametrics::getURL();

$tmp_start = microtime(true);
$usercount = file_get_contents($url."$username/contributions/type/stats/count");
//html_log('user contributions type stats count', microtime(true), $tmp_start);
$userlist = json_decode(file_get_contents($url."$username/contributions/type/stats/hits"), true);
$numcategory = count($userlist);

$tmp_start = microtime(true);
$worldcount = file_get_contents($url."contributions/stats/count");
//html_log('global stats count', microtime(true), $tmp_start);

/* 
$tmp_start = microtime(true);
$contributions = json_decode(file_get_contents($url."$username/contributions/time/values/"), true); 
html_log($url."$username/contributions/time/values/".' -- json decode time values', microtime(true), $tmp_start);

$tmp_start = microtime(true);
$days = json_decode(file_get_contents($url."$username/contributions/time/stats/dayth/"), true);
html_log($url."$username/contributions/time/stats/dayth/".' -- json decode dayth', microtime(true), $tmp_start);
*/
$tmp_start = microtime(true);	
$flickrurl = "http://www.flickr.com/photos/37983522@N02/";

$images = array();

$db = new PDO('sqlite:'.$DATABASE);

$sqlquery = "SELECT imei FROM user WHERE username = :username";
$stmt = $db->prepare($sqlquery);
$stmt->bindValue(':username', $username, PDO::PARAM_STR);
$stmt->execute();

$imei = $stmt->fetch();
if($imei != "" ){
    $imei = $imei[0];
}

$sqlquery = 'SELECT squareimg, url from images where username = :username and tags <> "notvalid"';
$stmt = $db->prepare($sqlquery);
$stmt->bindValue(':username', $imei, PDO::PARAM_STR);
$stmt->execute();
$images = $stmt->fetchall();

$images = array_slice($images, 0, 7);
html_log('flickr section ended', microtime(true), $tmp_start);

?>


<hr class="space" />    
<div class="span-4">
<?php
	$tmp_start = microtime(true);
	require("plant_list_user_page.php");
//	html_log('plant_list_user', microtime(true), $tmp_start);
?>
</div>

<div id="content" class="span-20 last">
<div class="span-20 center last">
	<h3>Welcome <?php echo $fullname; ?>, here are your specific stats. Have fun!</h3>
</div>

<div class="span-9 colborder">
<h3 style="text-align: center">Number of Images:</h4>
        <h3 class="color5 center"><?= $usercount ?></h3>
<h3 style="text-align: center">Percent Contributed:</h3>
        <h3 class="color5 center">
	<?php if ($worldcount): ?>
		<?= round($usercount / $worldcount, 2) * 100 ?>%
	<?php endif ?>
	</h3>
</div>

<div class="span-10 center last">
	<h3 class="center">Submissions:</h3>
        <img src="<?= $url ?><?= $username ?>/contributions/type/stats/hits.gc?cht=bhs&chs=200x<?=$numcategory*30+30?>&chco=8F4B21"/>
</div>
<!--
<div class="span-6 last">
        <h3 class="center">Last Contribution:</h3>
        <h3 class="color5 center">
	<?php if ($contributions): ?>
		<?= $contributions[0]['datetime'] ?>
	<?php endif ?>
	</h3>

        <h3 class="center">Days Contributed:</h3>
        <h3 class="color5 center">
	<?php if ($days): ?>
		<?= count($days) ?>
	<?php endif ?>
	</h3>
</div>
-->

<div class="span-20 last">
        <hr class="space" />
        <hr />
</div>
<div class="span-20 last">
        <div id="map_user_canvas" style="width: 790px; height: 400px"></div>
</div>
<hr class="space" />   
<!--
<div class="span-17 last">
        <h4>Your <a href="<?php echo $flickrurl;?>tags/<?php echo $imei;?>/">Images</a></h4>
	
	<?php foreach ($images as $image): ?>
	<div class="span-2"><a href="<?= $image['url'] ?>" target="_blank"><img src="<?= $image['squareimg'] ?>"></a></div>
	<?php endforeach ?>
</div>
-->
<hr class="space" />    
</div>

<?php 
//html_log('finished', microtime(true), $global_start);
?>
